package com.hilin.common.common;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@ConditionalOnClass(
        value = {RedisTemplate.class, RedisConnectionFactory.class}
)
public class RedisAutoConfig {

    @Bean
    public <T> RedisTemplate<String, T> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, T> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        JdkSerializationRedisSerializer serializer = new JdkSerializationRedisSerializer();
        // key采用StringRedisSerializer 这样在数据库中就没有双引号
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // 设置其他序列号引擎
        redisTemplate.setValueSerializer(serializer);
        redisTemplate.setHashKeySerializer(serializer);
        redisTemplate.setHashValueSerializer(serializer);
//        redisTemplate.setDefaultSerializer(serializer);
        return redisTemplate;
    }

}
